﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SL.M.Net
{
    /// <summary>
    /// Represent a frame the server sends to the client.
    /// </summary>
    public class ServerFrame<T> : GameObject where T : GameObject
    {
        T _serverObject;
        string _message;
        bool _faulted;

        /// <summary>
        /// Return or set the server object for this frame.
        /// </summary>
        public T ServerObject
        {
            get
            {
                return _serverObject;
            }
            set
            {
                _serverObject = value;
            }
        }

        /// <summary>
        /// Return or set the message being sent to the client.
        /// </summary>
        public string Message
        {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
            }
        }

        /// <summary>
        /// Return or set if the server is faulted and this is the last frame being sent to the client.
        /// </summary>
        public bool Faulted
        {
            get
            {
                return _faulted;
            }
            set
            {
                _faulted = value;
            }
        }

        /// <summary>
        /// Return or set the session id for the client sessions.
        /// </summary>
        public int SessionId
        {
            get;
            set;
        }

        /// <summary>
        /// Read this object from the given archiver.
        /// </summary>
        /// <param name="archiver">The archiver.</param>
        protected override void Read(Archiver archiver)
        {
            base.Read(archiver);
            archiver.ReadObject("_serverObject", ref _serverObject);
            _message = archiver.ReadString("_message");
            _faulted = archiver.ReadBool("_faulted");
            SessionId = archiver.ReadInt("SessionId");
        }

        /// <summary>
        /// Write this object to the given archiver.
        /// </summary>
        /// <param name="archiver">The archiver.</param>
        protected override void Write(Archiver archiver)
        {
            base.Write(archiver);
            archiver.WriteValue("_serverObject", _serverObject);
            archiver.WriteValue("_message", _message);
            archiver.WriteValue("_faulted", _faulted);
            archiver.WriteValue("SessionId", SessionId);
        }
    }
}
